/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JIFAbrirCaja.java
 *
 * Created on 09-nov-2011, 18:49:05
 */
package jam.sfcc.application;

import jam.sfcc.business.CajaBusiness;
import jam.sfcc.common.domain.Vendedor;
import java.sql.SQLException;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Josue
 */
public class JIFAbrirCaja extends javax.swing.JInternalFrame {
    private int EXITO=1,FALLO=2;
    private Vendedor vendedor;

    public JIFAbrirCaja(Vendedor vendedor) {
        initComponents();
        this.vendedor=vendedor;
        CajaBusiness cajaBusiness=new CajaBusiness(vendedor.getNombre(), vendedor.getClave());
        int resultado=EXITO;
        try {
            float montoDeCierre=cajaBusiness.getCierreDeCajaAnterior().getMontoDeCierre();
            jtfMontoInicial.setText(String.valueOf(montoDeCierre));
            jtfFecha.setText((new Date().toLocaleString()));
            jtfVendedor.setText(vendedor.getNombre()+" "+vendedor.getApellidos());
        } catch (SQLException ex) {
            resultado=FALLO;
        }
        if(resultado==FALLO){
            JOptionPane.showMessageDialog(this, "Ha ocurrido un error a la hora de \n"
                                              + "cargar los datos de caja.");
        }
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelEncabezados1 = new jam.sfcc.application.PanelEncabezados();
        jLabel11 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jtfMontoInicial = new javax.swing.JTextField();
        jbAbrirCaja = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jtfFecha = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtfVendedor = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setTitle("Abrir Caja");

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel11.setText("Abrir Caja");

        javax.swing.GroupLayout panelEncabezados1Layout = new javax.swing.GroupLayout(panelEncabezados1);
        panelEncabezados1.setLayout(panelEncabezados1Layout);
        panelEncabezados1Layout.setHorizontalGroup(
            panelEncabezados1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelEncabezados1Layout.createSequentialGroup()
                .addGap(106, 106, 106)
                .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                .addGap(129, 129, 129))
        );
        panelEncabezados1Layout.setVerticalGroup(
            panelEncabezados1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelEncabezados1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                .addGap(20, 20, 20))
        );

        jLabel1.setText("Monto Inicial en Colones:");

        jbAbrirCaja.setText("Abrir Caja");
        jbAbrirCaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAbrirCajaActionPerformed(evt);
            }
        });

        jLabel2.setText("Fecha:");

        jLabel3.setText("Vendedor:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelEncabezados1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(154, 154, 154)
                        .addComponent(jbAbrirCaja))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(76, 76, 76)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtfFecha)
                            .addComponent(jtfVendedor)
                            .addComponent(jtfMontoInicial, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelEncabezados1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfVendedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfMontoInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addComponent(jbAbrirCaja)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbAbrirCajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAbrirCajaActionPerformed
        float montoInicial= Float.parseFloat(jtfMontoInicial.getText());
        CajaBusiness cajaBusiness=new CajaBusiness(vendedor.getNombre(), vendedor.getClave());
        int resultado=EXITO;
        try {
            cajaBusiness.abrirCaja(montoInicial, vendedor);
            
        } catch (SQLException ex) {
            resultado=FALLO;
        }
        if(resultado==EXITO){
            JOptionPane.showMessageDialog(this, "Se ha abierto la caja con éxito");
        }else{
            JOptionPane.showMessageDialog(this, "No se ha podido abrir caja");
        }
    }//GEN-LAST:event_jbAbrirCajaActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton jbAbrirCaja;
    private javax.swing.JTextField jtfFecha;
    private javax.swing.JTextField jtfMontoInicial;
    private javax.swing.JTextField jtfVendedor;
    private jam.sfcc.application.PanelEncabezados panelEncabezados1;
    // End of variables declaration//GEN-END:variables
}
